using UnityEngine;
using System.Collections;

/// \class  gkScreenTransform
/// \brief 	Screen transform
[AddComponentMenu("GK/Engine/Core/2D/Transform/gkScreenTransform")]
public class gkScreenTransform : MonoBehaviour
{	
	/// The position in pixel relativly to the screen top left
	private Vector2 m_f2PositionInPixel;
	public Vector2 positionInPixel
	{
		get
		{
			return m_f2PositionInPixel;
		}
		
		set
		{
			m_f2PositionInPixel = value;
			UpdateRectangleFromPositionAndSize();
		}
	}
	
	/// The size in pixel
	private Vector2 m_f2SizeInPixel;
	public Vector2 sizeInPixel
	{
		get
		{
			return m_f2SizeInPixel;
		}
		
		set
		{
			m_f2SizeInPixel = value;
			UpdateRectangleFromPositionAndSize();
		}
	}
	
	/// The rectangle bounds
	Rect m_oRectangleBounds;
	public Rect rectangleBounds
	{
		get
		{
			return m_oRectangleBounds;
		}
		
		set
		{
			m_oRectangleBounds = value;
			UpdatePositionAndSizeFromRectangle();
		}
	}
	
	/// \brief  Test if the screen transform contain a screen point
	public bool ContainsScreenPoint(Vector2 a_f2ScreenPosition)
	{
		return m_oRectangleBounds.Contains(a_f2ScreenPosition);
	}
	
	/// \brief  Update the Rectangle from the position
	void UpdateRectangleFromPositionAndSize()
	{		
		// Update the size
		m_oRectangleBounds.width = m_f2SizeInPixel.x;
		m_oRectangleBounds.height = m_f2SizeInPixel.y;
		
		// Update the left up corner position
		m_oRectangleBounds.x = m_f2PositionInPixel.x - m_f2SizeInPixel.x * 0.5f;
		m_oRectangleBounds.y = m_f2PositionInPixel.y - m_f2SizeInPixel.y * 0.5f;
	}
	
	/// \brief  Update the position from the rectangle
	void UpdatePositionAndSizeFromRectangle()
	{		
		// Update the size
		m_f2SizeInPixel.x = m_oRectangleBounds.width;
		m_f2SizeInPixel.y = m_oRectangleBounds.height;
		
		// Update the position
		m_f2PositionInPixel.x = m_oRectangleBounds.x + m_oRectangleBounds.width * 0.5f;
		m_f2PositionInPixel.y = m_oRectangleBounds.y + m_oRectangleBounds.height * 0.5f;
	}
}